package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ArticleAddDTO;
import org.jsola.hr.dto.ArticleUpdateDTO;
import org.jsola.hr.query.ArticleQuery;
import org.jsola.hr.service.IArticleService;
import org.jsola.hr.vo.ArticleListVO;
import org.jsola.hr.vo.ArticleVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 人力文章
 *
 * @author zhr
 */
@RestController("hrArticleController")
@RequestMapping(HrConstants.API_PATH + "/v1/articles")
public class ArticleController implements IArticleControllerAPI {

    @Autowired
    private IArticleService articleService;

    @Override
    @PostMapping
    public Result<ArticleVO> save(@Valid @RequestBody ArticleAddDTO articleAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增人力文章权限校验

        return Result.success(
                articleService.save(articleAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ArticleUpdateDTO articleUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改人力文章权限校验

        int count = articleService.update(articleUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{articleId}")
    public Result<Integer> delete(@PathVariable Long articleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除人力文章权限校验

        int count = articleService.deleteByIds(tokenUser, articleId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] articleIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除人力文章权限校验

        int count = articleService.deleteByIds(tokenUser, articleIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{articleId}")
    public Result<ArticleVO> findById(@PathVariable Long articleId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力文章权限校验

        return Result.success(
            articleService.selectById(articleId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ArticleListVO>> listAll(@Valid ArticleQuery articleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人力文章权限校验

        return Result.success(
            articleService.select(articleQuery, null)
        );
    }


    @Override
    @GetMapping
    public Result<Page<ArticleListVO>> page(@Valid ArticleQuery articleQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            articleService.selectPage(articleQuery, null)
        );
    }


}

